﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SAD_SiS.Models;
using System.Web.Security;

namespace SAD_SiS.Controllers
{
    public class AccountController : Controller
    {
        //
        // GET: /Account/
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Login(FormCollection form)
        {
            string username = form["username"];
            string password = form["password"];
            using (SADEntities dbContext = new SADEntities())
            {
                var account = (from acc in dbContext.Accounts
                               where acc.Username.Equals(username)
                                   && acc.Userpass.Equals(password)
                               select acc).SingleOrDefault();
                if (account != null)
                {
                    Session["login"] = account.Username;
                    FormsAuthentication.SetAuthCookie(account.Username, false);
                    if (account.RoleID == 3)
                    {
                        return RedirectToAction("Index", "Product");
                    }
                    else if (account.RoleID==2)
                    {
                        return RedirectToAction("Index","ShopHome"); 
                    }
                    
                }
                return RedirectToAction("Index", "Home");
            }
        }
        public ActionResult LogOut()
        {
            return View();
        }
        public ActionResult Profile(string strUser)
        {
            using (SADEntities dbContext = new SADEntities())
            {
                var account = (from acc in dbContext.Accounts where acc.Username.Equals(strUser) select acc).Single();
                return View(account);
            }
        }
        public ActionResult FogotPws(FormCollection form)
        {
            return View();
        }
        public ActionResult Register()
        {
            return View();
        }
    [HttpPost]
        public ActionResult Register(FormCollection form)
        {
        string username=form["username"];
        if (username != null)
            {
                return RedirectToAction("DeliverInfor");
            }
            else
            {
                return View();
            }
        }
        public ActionResult DeliverInfor() { return View(); }
        public ActionResult RegisterShop() { return View(); }
    }
}
